import CommonAPI from "@/api/common";
import PaymentAPI from "@/api/payment";

interface DictOption {
  value: string;
  label: string;
}

export interface Dict {
  allPaTypesList: DictOption[];
  allSeparateTypesList: DictOption[];
  channelTypeList: DictOption[];
  statusList: DictOption[];
  settleTypesList: DictOption[];
}

const KEY_MAP = {
  channelTypeList: { label: "channelDesc", value: "channelType" },
  statusList: { label: "statusName", value: "status" },
  settleTypesList: { label: "statusName", value: "status" },
};

export const useDict = (callback?: (dict: Dict) => void) => {
  let dict = reactive({});

  const getData = () => {
    Promise.all([CommonAPI.getEnums(), PaymentAPI.getOrderEnum()]).then(
      ([res, res2]) => {
        const all = { ...res, ...res2 };
        const result = {};
        for (const key in all) {
          // @ts-ignore
          const { label = "name", value = "value" } = KEY_MAP?.[key] ?? {};
          let arr = [];
          arr = all[key].map((t: any) => ({
            value: t[value],
            label: t[label],
          }));
          // @ts-ignore
          result[key] = arr;
        }
        dict = result;
        callback && callback(dict as Dict);
      }
    );
  };

  onMounted(() => {
    getData();
  });
  return { dict };
};
